<?php

namespace fq\request;

class RequestClient extends BaseService
{

	private $appKey;//你的apikey
	private $app_secret;//你的密钥
	protected $request_url = 'https://v3.api.haodanku.com/rest';
	protected $interface;
	public $method;

	public function __construct($appKey, $app_secret)
	{
		$this->appKey = $appKey;
		$this->app_secret = $app_secret;
	}

	/**
	 * @param string $interface
	 * @param string $method
	 */
	public function load($interface = '', $method = 'GET')
	{
		$this->method = $method;
		$this->interface = $interface;
	}


	public function request_interface($data = [])
	{
		date_default_timezone_set("Asia/Shanghai");
		$field['app_id'] = $this->appKey;
		$field['method'] = $this->interface;
		$field['date'] = date('Y-m-d H:i:s');
		$PostField = array_merge($field, $data);
		$this->sign_create($PostField, $this->app_secret);
		$header = array('content-type: application/json;charset=utf-8');
		$url = $this->request_url;
		if ($this->method == 'GET') {
			$url .= $this->createStrParam($PostField);
		}
		return $this->curl($url, $this->method, $PostField, $header);
	}


	private function sign_create(&$data, $secret = '')
	{
		ksort($data);
		$stringToBeSigned = '';
		foreach ($data as $k => $v) {
			if (!is_array($v) && "@" != substr($v, 0, 1)) {
				$stringToBeSigned .= "$k$v";
			}
		}
		$sign = strtoupper(md5($stringToBeSigned . $secret));
		$data['sign'] = $sign;
	}
}